
#include "Comm.h"

#include <windows.h>
#include <iostream>

#include "Sim.h"
#include "Graphics.h"
#include "TitleMenu.h"
#include "GameInitInfo.h"
#include "Utils.h"
//#include "ServerGame.h";
//#include "ClientGame.h";

using namespace std;


//*----------------------------------------------------------------------------* /
//DWORD startSim(LPVOID) {
//	sim->start();
//	return 0;
//}*/
//
//*----------------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
	
	Sim* sim;
	Graphics* gfx;
	TitleMenu* tmenu;
	GameInitInfo gii;
	
	//start the title menu and 
	//get the game parameters
	tmenu = new TitleMenu();
	gii = tmenu->startTitle();
	delete tmenu;
	
	
	//Launch Server Connection Listener
	if(gii.isHost) {
		//start the sim and pass it the number of
		//moons from the title menu
		sim = new Sim(gii.moons, 100);
		//ServerGame * sgame = new ServerGame(gii.moons);
		//(*sim).setServerGame(sgame);

		//Setup and Assign the Server Connection Recvr args.  This only get the sim it know the port it is static
		portIPSim* serverConnArgs = new portIPSim(0,"",sim);

		DWORD threadId;
		HANDLE simThread = CreateThread(NULL,0,
			(LPTHREAD_START_ROUTINE)Comm::ServerConnectionRecvr,serverConnArgs,0,&threadId);
		if ( simThread == NULL ) { cout << "SimThread Creation Failed!" << endl; }
	} else {
		//Connect to a server
		try {

			//start the "thinSim"
			sim = new Sim();
			//ClientGame * cgame = new ClientGame(0);
			//(*sim).setClientGame(cgame);


			const string ip = gii.IP;
			SocketClient s = SocketClient(ip, Comm::ServerStartingPort);	//ZK070213: changed this to ServerStartingPort	
			s.SendLine("JoinGame");
			
			string ports = s.ReceiveLine();
			cout << ports;
			
			vector<string>parsedPorts;
			Utils::SplitString(ports, ",", parsedPorts, false);
			s.Close();
			
			//Setup and Assign Arguments
			portIP* senderArgs = new portIP(atoi(parsedPorts[1].c_str() ), ip);
			portIPSim* recvrArgs = new portIPSim(atoi(parsedPorts[0].c_str() ), ip, sim);

			//Launch threads
			/*DWORD SenderThreadId;*/
			DWORD RecvrThreadId;
			//Launch a sender for the client
			/*HANDLE ClientSenderThread = CreateThread(NULL,0,
				(LPTHREAD_START_ROUTINE)Comm::ClientSender,senderArgs,0,&SenderThreadId);
			if ( ClientSenderThread == NULL ) { cout << "ServerSender Creation Failed!" << endl; }*/
			
			//Launch a recvr thread for the client
			HANDLE ClientRecvrThread = CreateThread(NULL,0,
				(LPTHREAD_START_ROUTINE)Comm::ClientRecvr,recvrArgs,0,&RecvrThreadId);
			if ( ClientRecvrThread == NULL ) { cout << "ServerRecr Creation Failed!" << endl; }
			
			//Comm::ClientSender(senderArgs);

			while(!sim->isThinSimRdy());
		}
		catch ( string str ) { cerr << str << endl; }
		catch ( ... ) { cerr << "unhandled exception\n"; }
	}

	//start the graphics and pass it the width
	//and height from the title menu.
	gfx = new Graphics(gii.width, gii.height, sim);

	/*DWORD threadId;
	HANDLE simThread = CreateThread(NULL,0,
		(LPTHREAD_START_ROUTINE)startSim,NULL,0,&threadId);
	if ( simThread == NULL ) { cout << "SimThread Creation Failed!" << endl; }

	gfx->start();

	WaitForSingleObject(simThread, INFINITE);*/

	return 0;
}


/*----------------------------------------------------------------------------* /
DWORD startSim(LPVOID) {
	sim->start();
	return 0;
}*/

/*----------------------------------------------------------------------------*/
/*int main(int argc, char *argv[]) {
	//testing all my game stuff
	ServerGame * game = new ServerGame(5);
	(*game).addPlayer(1, "Chris");
	(*game).addPlayer(2, "Zach");
	(*game).addPlayer(3, "Jeremy");
	cout << (*game).serialize();
	(*game).update();
	(*game).update();
	(*game).update();
	(*game).update();
	(*game).update();
	cout << (*game).serialize();

	ClientGame * clientGame = new ClientGame(2);
	(*clientGame).update((*game).serialize());
	int i;
	cin >> i;


}*/